package utilities;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class registracijaServlet
 */
@WebServlet("/registracijaServlet")
public class registracijaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public registracijaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String ime = request.getParameter("ime");
		String priimek = request.getParameter("priimek");
		String spol = request.getParameter("spol");
		String naslov = request.getParameter("naslov");
		String letor = request.getParameter("letor");
		String letod = request.getParameter("letod");
		String vrstas = request.getParameter("vrstas");
		String stopnjas = request.getParameter("stopnjas");
		String studijskas = request.getParameter("studijskas");
		String email = request.getParameter("email");
		String geslo = request.getParameter("geslo");
		String pgeslo = request.getParameter("pgeslo");
		
		HttpSession session = request.getSession();
		
		Helper.vrniPolja(session, ime, priimek, spol, naslov, letor, letod, vrstas, stopnjas, studijskas, email, geslo, pgeslo);
		
		if (Helper.preveriPolja(ime, priimek, spol, naslov, letor, letod, vrstas, stopnjas, studijskas, email, geslo, pgeslo)) {
			if (Helper.preveriEmail(email)) {
				if (Helper.preveriGeslo(geslo, pgeslo)){
					session.setAttribute("uporabnik", new Uporabnik(ime, priimek, spol, naslov, letor, letod, vrstas, stopnjas, studijskas, email, geslo, "user"));
					response.sendRedirect("index.jsp");
				}else {
					session.setAttribute("errorReport", "Geslo in potrditveno geslo se ne ujemata. Prosimo popravite.");
					response.sendRedirect("registracija.jsp");
				}
			}else {
				session.setAttribute("errorReport", "E-mail ni pravilno napisan. Prosimo popravite.");
				response.sendRedirect("registracija.jsp");
			}
		}else {
			session.setAttribute("errorReport", "Niste izpolnili vseh polj. Prosimo dopolnite.");
			response.sendRedirect("registracija.jsp");
		}

		//session.setAttribute("errorReport", "Forma ni pravilino izpolnjena. Prosimo popravite.");
		response.sendRedirect("registracija.jsp");
	}

}
